
import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: app.imageBaseURL,
    imageResizeParams: app.imageResizeParams,
    list:[],
    youthid:'',
    keywords:'',
    page:1,
    pagesize:10,
    searchnull:false,
  },
  //搜索
  onSearch(e){
    this.setData({
      keywords:e.detail,
      page:1,
      pagesize:10,
    })
    this.getActiveList(this.data.youthid,this.data.keywords)
  },
  onCancel(e){
    this.setData({
      keywords:e.detail,
      page:1,
      pagesize:10,
    })
    this.getActiveList(this.data.youthid,this.data.keywords)
  },
  //最新活动
  async getActiveList (youthid,keywords) {
    let data={}
    data['flag'] = '_club_more_activity'
    data['cityName'] ='北京市'
    data['cityCode'] =''
    data['location[lng]'] = app.globalData.currpoint.longitude
    data['location[lat]'] = app.globalData.currpoint.latitude
    data['page'] = this.data.page
    data['pagesize'] = this.data.pagesize
    data['label'] = 0
    data['youthid'] = youthid
    data['keywords'] = keywords
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
        result && result.forEach((item,index) => {
          item.imgurl[0] = this.loadImg(item.imgurl[0])
        })
      
      
        if( result && result.length>0 ){
          let page = this.data.page+1
          let str = 'list['+(this.data.page-1)+']'
          console.log(str)
          this.setData({
            [str]:result,page
          })
        }
        if(this.data.list.length == 0 ){
          this.setData({
            searchnull:true
          })
        }
    }else {
      if(this.data.list.length == 0 ){
        this.setData({
          searchnull:true
        })
      }
      // Toast(message)
    }
  },
  //图片加载
  loadImg(img){
    let url =  api.checkurl(img)
    return url
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      youthid:options.youthid
    })
    this.getActiveList(this.data.youthid,this.data.keywords)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getUser()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getActiveList(this.data.youthid,this.data.keywords)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})